package cn.edu.thsis.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;

import cn.edu.thsis.pojo.User;
import cn.edu.thsis.pojo.student;
import cn.edu.thsis.pojo.teacher;
import cn.edu.thsis.service.UserService;
import cn.edu.thsis.service.impl.UserServiceImpl;

/**
 * Servlet implementation class UpdateTea
 */
@WebServlet("/UpdateUser")
public class UpdateUserServlet extends HttpServlet {
	//获取servlet层对象
	UserService tea = new UserServiceImpl();
	UserService stu = new UserServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;utf-8");
		String oper = req.getParameter("oper");
		String id = req.getParameter("id");
		int success = 0;
		if ("addtea".equals(oper)) {//添加教师
			addtea(req,resp);
		}else if ("deltea".equals(oper)) {//删除教师
			try {
				success = deltea(id);
			} catch (Exception e) {}
			if(success!=0) {
				req.getRequestDispatcher("/UpdateUser?oper=showtea").forward(req, resp);
			}
		}else if("showtea".equals(oper)){//获取老师信息
			try {
				showtea(req,resp);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		else if ("addstu".equals(oper)) {//添加学生
			addstu(req,resp);
		}else if ("delstu".equals(oper)) {//删除学生信息
			try {
				success = delstu(id);
			} catch (SQLException e) {
				e.printStackTrace();
			}if(success!=0) {
				req.getRequestDispatcher("/UpdateUser?oper=showstu").forward(req, resp);}
		}else if ("showstu".equals(oper)) {//查看学生信息
			try {
				showstu(req,resp);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if ("stuth".equals(oper)) {//查看未选题的学生
			try {
				stuth(req,resp);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if ("teath".equals(oper)) {//查看未出题的教师
			try {
				teath(req,resp);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if ("warnstu".equals(oper)) {
			try {
				warnstu(req,resp,id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if ("warntea".equals(oper)) {
			try {
				warntea(req,resp,id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//催促教师按时出题
	private void warntea(HttpServletRequest req, HttpServletResponse resp, String id) throws ServletException, SQLException {
		int index = tea.warnteaService(id);
		if (index>0) {
			try {
				req.getRequestDispatcher("/UpdateUser?oper=teath").forward(req, resp);
			} catch (IOException e){
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	//催促学生按时选题
	private void warnstu(HttpServletRequest req, HttpServletResponse resp, String id) throws ServletException, SQLException {
		int index = stu.warnService(id);
		if (index>0) {
			try {
				req.getRequestDispatcher("/UpdateUser?oper=stuth").forward(req, resp);
			} catch (IOException e){
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	//查看未出题的教师
	private void teath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
		List<teacher> teath = tea.teathService();
		req.setAttribute("teath", teath);
		req.getRequestDispatcher("admin/admin_teath.jsp").forward(req, resp);
	}

	//查看未选题的学生
	private void stuth(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
		List<student> stuth = stu.stuthService();
		req.setAttribute("stuth", stuth);
		req.getRequestDispatcher("admin/admin_stuth.jsp").forward(req, resp);
	}

	//查看学生信息
	private void showstu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
		List<student> stulist = stu.stuShowService();
		req.setAttribute("stulist", stulist);
		HttpSession session = req.getSession();
		String uid = ((User) session.getAttribute("user")).getUid();
		String type = (String) session.getAttribute("type");
		if (type.equals("admin")) {
			req.getRequestDispatcher("admin/admin_stu.jsp").forward(req, resp);
		}else {
			req.getRequestDispatcher("teacher/tea_allstu.jsp").forward(req, resp);
		}
	}

	//查看教师信息
	private void showtea(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
		List<teacher> tealist = tea.teaShowService();
		req.setAttribute("tealist", tealist);
		HttpSession session = req.getSession();
		String uid = ((User) session.getAttribute("user")).getUid();
		String type = (String) session.getAttribute("type");
		if (type.equals("admin")) {
		req.getRequestDispatcher("admin/admin_tea.jsp").forward(req, resp);}
		else {
			req.getRequestDispatcher("student/tea_list.jsp").forward(req, resp);
		}
	}

	//删除学生信息
	private int delstu(String uid) throws SQLException {
		int index = stu.delstuService(uid);
		return index;
	}

	//删除教师信息
	private int deltea(String uid) throws SQLException {
		int index = tea.delteaService(uid);
		return index;
	}


	//添加学生信息
	private void addstu(HttpServletRequest req, HttpServletResponse resp) throws ServletException{
		String uid = req.getParameter("uid");
		String uname = req.getParameter("uname");
		String major =req.getParameter("major");
		String stu_tel = req.getParameter("stu_tel");
		String stu_email = req.getParameter("stu_email");
		student stud =new student();
		stud.setUid(uid);
		stud.setUname(uname);
		stud.setMajor(major);
		stud.setStu_tel(stu_tel);
		stud.setStu_email(stu_email);
		int index = stu.addstuService(stud);
		if (index>0) {
			try {
				req.getRequestDispatcher("/UpdateUser?oper=showstu").forward(req, resp);
			} catch (IOException e){
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	//添加教师信息
	private void addtea(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
		String uid = req.getParameter("uid");
		String uname = req.getParameter("uname");
		String tea_title =req.getParameter("tea_title");
		String tea_tel = req.getParameter("tea_tel");
		teacher teahr = new teacher();
		teahr.setUid(uid);
		teahr.setUname(uname);
		teahr.setTea_title(tea_title);
		teahr.setTea_tel(tea_tel);
		int index = tea.addteaService(teahr);
		if (index>0) {
			try {
				req.getRequestDispatcher("/UpdateUser?oper=showtea").forward(req, resp);
			} catch (IOException e){
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
